<template>
  <a-modal
    title="添加商品"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item v-show="false" label="主键ID">
          <a-input v-decorator="['id', { initialValue: model?.id }]" disabled />
        </a-form-item>
        <a-form-item v-show="false" label="goodName">
          <a-input v-decorator="['goodName']" disabled />
        </a-form-item>
        <a-form-item v-show="false" label="inventoryId">
          <a-input v-decorator="['inventoryId']" disabled />
        </a-form-item>
        <a-form-item v-show="false" label="phoneCode1">
          <a-input v-decorator="['phoneCode1']" disabled />
        </a-form-item>
        <a-form-item v-show="false" label="phoneCode2">
          <a-input v-decorator="['phoneCode2']" disabled />
        </a-form-item>
        <a-form-item label="选择商品">
          <a-select
            show-search
            :value="value ? value : undefined"
            :placeholder="`请至少输入一个字符`"
            :default-active-first-option="false"
            :show-arrow="false"
            :filter-option="false"
            @select="handleSelect"
            :not-found-content="null"
            @search="handleSearch"
            @change="handleChange"
          >
            <a-select-option v-for="d in goodsoption" :key="d.inventoryId">
              {{ d.goodName }} {{ d.phoneCode1 }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="金额">
          <a-input
            v-decorator="['price', { initialValue: model?.price }]"
            addonAfter="元"
            placeholder="请输入调整的金额"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>
  
  <script>
import { getInventoryListByPhoneCdeUsingGET } from '@/api/api/pifayewu.ts'
import pick from 'lodash.pick'
// 表单字段
const fields = ['price', 'goodName','inventoryId','phoneCode1','phoneCode2','id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 },
      },
    }
    return {
      goodsoption: [],
      optionParam: {
        pageNo: 1,
        pageSize: 999,
        queryCondition: '',
        status: 1,
      },
      value:undefined,
      form: this.$form.createForm(this),
    }
  },
  created() {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    handleSelect(e){
        this.form.setFieldsValue({
            goodName:this.goodsoption.find((item) => {
              return item.inventoryId == e
            })?.goodName,
            inventoryId:this.goodsoption.find((item) => {
              return item.inventoryId == e
            })?.inventoryId,
            phoneCode1:this.goodsoption.find((item) => {
              return item.inventoryId == e
            })?.phoneCode1,
            phoneCode2:this.goodsoption.find((item) => {
              return item.inventoryId == e
            })?.phoneCode2,
        })
    },
    handleSearch(value) {
      this.getGoodsOption(value)
    },
    handleChange(value) {
      this.value = value
    },
    getGoodsOption(params) {
      getInventoryListByPhoneCdeUsingGET({ ...this.optionParam, storeId: this.$route.query.storeId, phoneCodeStr: params })
        .then((res) => {
          this.goodsoption = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
  },
}
</script>
  